-- UISkillTooltip
-- Created by chengb May/27/2015
-- 技能悬浮界面

UISkillTooltip = class("UISkillTooltip", function()
    return cc.Layer:create();
end);

function UISkillTooltip.create(skillId, requireMp)
    return UISkillTooltip.new(skillId, requireMp);
end

-- 背景框切片数量
local SMALL_BG_COUNT = 4;

-- 构造函数
function UISkillTooltip:ctor(skillId, requireMp)
    -- 初始化
    self:setName("UISkillTooltip");
    local node = cc.CSLoader:createNode("layout/hero/SkillTooltip.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录技能编号和魔法消耗
    self.skillId   = skillId;
    self.requireMp = requireMp;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();

    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UISkillTooltip 界面析构清理");
        end
    end);
end

-- 适配
function UISkillTooltip:resize(node)
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册点击事件
function UISkillTooltip:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISkillTooltip");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 点击任意地方返回
    local bg = findChildByName(self.node, "bg/bgImg");
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISkillTooltip");
        end
    end
    bg:addTouchEventListener(onBgClick);

    -- 防止点击事件被吞掉
    local bg = findChildByName(self.node, "CT/bg");
    for i = 1, SMALL_BG_COUNT do
        local subBg = findChildByName(bg, "bg" .. i);
        if subBg ~= nil then
            subBg:setSwallowTouches(false);
        end
    end
end

-- 重绘
function UISkillTooltip:redraw()
    -- 技能名称
    local skillId = self.skillId;
    local skillName = SkillM.query(skillId, "name");
    local nameLabel = findChildByName(self.node, "CT/name");
    TextStyleM.setTitleStyle(nameLabel);
    nameLabel:setString(skillName);

    -- 技能描述
    local desc = SkillM.getSkillDesc(skillId);
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 消耗描述
    if SlimeSkillM.isSlimeSkill(skillId) then
        local costDesc = SlimeSkillM.getCostDesc(skillId);

        if costDesc then
            desc = string.format("%s\n\n%s", desc, costDesc);
        end
    end

    descLabel = applyString(descLabel, desc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_SMALL, { nameLabel, descLabel, },
        { 1.5*AlignM.DEFAULT_GAP }, AlignM.DEFAULT_MARGIN, 1.5*AlignM.DEFAULT_MARGIN );

    AlignM.adaptSubBg(self.node, AlignM.BG_SIZE_SMALL, 1.5);
end






